package reflect;
/**
 * 通过反射机制调用有参方法
 */

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo5 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个类名：");
        String className = sc.nextLine();
        System.out.println("请输入一个方法名");
        String methodName = sc.nextLine();

        Class cls = Class.forName(className);//获取类对象
        Constructor constructor = cls.getConstructor(String.class);//获取类的构造器
        Object obj = constructor.newInstance("张三");//new对象

        Method method1 = cls.getMethod("say",String.class);
        method1.invoke(obj,"大家好");  //obj.say("大家好")

        Method method2 = cls.getMethod("say",String.class,int.class);
        method2.invoke(obj,"大家好",5);  //obj.say("大家好",5);

    }
}
